﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
using System.Threading;
using Sendmail.Server;
using log4net;
using log4net.Config;

namespace Sendmail.Console
{
    class ServerConsole
    {
        private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private TcpChannel channel;
        public SmtpServer SmtpServer1 { get; private set; }

        static void Main()
        {
            XmlConfigurator.Configure();

            var handler1 = new ServerConsole();
            var thread1 = new Thread(handler1.Run);
            thread1.Start();

            Log.Debug("Application loaded successfully.");
            System.Console.ReadKey();
        }

        public void Run()
        {
            SmtpServer1 = new SmtpServer(IPAddress.Any, 25);

            BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
            provider.TypeFilterLevel = TypeFilterLevel.Full;
            IDictionary props = new Hashtable();
            props["port"] = 9934;
            channel = new TcpChannel(props, null, provider);
            ChannelServices.RegisterChannel(channel);
            // RemotingConfiguration.RegisterWellKnownServiceType(typeof(CustomType), "CustomType", WellKnownObjectMode.Singleton);
            RemotingServices.Marshal(SmtpServer1, "SendMailSvc");

        }
    }
}
